#include <iostream>
using namespace std;

struct LinkNode
{
    int data;
    LinkNode *next;
    LinkNode(int x) : data(x), next(NULL)
    {
    }
};

LinkNode *reverseLinkList(LinkNode *head)
{
    LinkNode *pre = NULL;
    LinkNode *cur = head;
    LinkNode *next = NULL;
    while (cur != NULL)
    {
        // 保存当前节点的下一个节点
        next = cur->next;
        // 将当前节点的next指针指向前一个节点
        cur->next = pre;
        // 更新前一个节点为当前节点
        pre = cur;
        // 更新当前节点为下一个节点
        cur = next;
    }
    // 返回新的头结点
    return pre;
}

void print(LinkNode *head)
{
    LinkNode *cur = head;
    while (cur != NULL)
    {
        cout << cur->data << " ";
        if (cur->next != NULL)
        {
            cout << "->";
        }
        cur = cur->next;
    }
    cout << endl;
}

int main()
{
    LinkNode *head = new LinkNode(1);
    head->next = new LinkNode(2);
    head->next->next = new LinkNode(3);
    print(head);
    head = reverseLinkList(head);
    print(head);
}
